package com.swt.testdemo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.config.ResponseMessage;
import com.swt.testdemo.config.ServerConfig;
import com.swt.testdemo.dao.DictCityDao;
import com.swt.testdemo.dao.DictCountryDao;
import com.swt.testdemo.dao.DictProvinceDao;
import com.swt.testdemo.entity.DictCityEntity;
import com.swt.testdemo.entity.DictCountyEntity;
import com.swt.testdemo.entity.DictProvinceEntity;
import com.swt.testdemo.service.SysCommonService;
import com.swt.testdemo.service.SysDictCityService;
import com.swt.testdemo.service.SysDictCountyService;
import com.swt.testdemo.service.SysDictProvinceService;
import com.swt.testdemo.util.RedisUtil;
import com.swt.testdemo.vo.DictCityVO;
import com.swt.testdemo.vo.DictCountyInfoVO;
import com.swt.testdemo.vo.DictCountyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/19 10:08
 **/
@Service("SysCommonService")
public class SysCommonServiceImpl implements SysCommonService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private DictCountryDao dictCountryDao;

    @Autowired
    private DictProvinceDao dictProvinceDao;

    @Autowired
    private SysDictCountyService sysDictCountyService;

    @Autowired
    private SysDictCityService sysDictCityService;

    @Autowired
    private SysDictProvinceService sysDictProvinceService;


    @Autowired
    private DictCityDao dictCityDao;


   private static Map<String,DictProvinceEntity> dictProvinceEntityMap = new HashMap<>();

   private static Map<String, DictCityEntity> dictCityEntityMap = new HashMap<>();

   private static Map<String, DictCountyEntity> dictCountyEntityMap  = new HashMap<>();





    @Override
    public ResponseBean getSystemFileServer() {
        HttpStatus status = HttpStatus.OK;

        String systemFileServe = StrUtil.format("http://{}:{}", serverConfig.getAddress(), serverConfig.getPort());

        ResponseBean bean = new ResponseBean(status.value(),"查询成功",systemFileServe);
        return bean;
    }

    @Override
    public ResponseBean getDictCountryInfo(String countyFullCode) {

        String key = "dict:county:"+countyFullCode;
        DictCountyInfoVO dictCountyInfoVO = redisUtil.getObject(key, DictCountyInfoVO.class);

        if (dictCountyInfoVO !=null){
            return new ResponseBean(200,"查询成功",dictCountyInfoVO);
        }

        //获取城区名
        DictCountyEntity dictCountyEntity = dictCountryDao.selectByFullCode(countyFullCode);
        Integer provinceId = dictCountyEntity.getProvinceId();


        //获取市级城市名
        String cityFullCode = StrUtil.sub(countyFullCode,0,4);
        DictCityEntity dictCityEntity = dictCityDao.selectByCityFullCode(cityFullCode);


        //获取省级城市名
        DictProvinceEntity dictProvinceEntity = dictProvinceDao.selectById(provinceId);

        dictCountyInfoVO = new DictCountyInfoVO();

        dictCountyInfoVO.setCityCode(dictCityEntity.getFullCode());
        dictCountyInfoVO.setCityName(dictCityEntity.getCityName());
        dictCountyInfoVO.setCountyCode(dictCountyEntity.getFullCode());
        dictCountyInfoVO.setCountyName(dictCountyEntity.getCountyName());
        dictCountyInfoVO.setProvinceCode(dictProvinceEntity.getProvinceCode());
        dictCountyInfoVO.setProvinceName(dictProvinceEntity.getProvinceName());


        return new ResponseBean(200,"查询成功",dictCountyInfoVO);
    }

    @Override
    public ResponseBean getDictProvinceList() {
        List<DictProvinceEntity> dictProvinceEntityList = dictProvinceDao.findAll();
        Map<String, String> dictProvinceMap = CollUtil.fieldValueAsMap(dictProvinceEntityList, "provinceCode", "provinceName");
        return new ResponseBean(200,"查询成功",dictProvinceMap);

    }

    @Override
    public ResponseBean getDictProvinceCity(String provinceCode) {
        String key = "dict:province:city:"+provinceCode;
        List<DictCityVO> dictCityVOList = redisUtil.getArray(key, DictCityVO.class);

        if (dictCityVOList !=null){
            return new ResponseBean(200,"查询成功",dictCityVOList);
        }

        DictProvinceEntity dictProvinceEntity = dictProvinceDao.selectByProvinceCode(provinceCode);
        Integer dictProvinceId = dictProvinceEntity.getId();

        List<DictCityEntity> dictCityEntityList = dictCityDao.selectByProvinceId(dictProvinceId);



       dictCityVOList = dictCityEntityList.stream().map(dictCityEntity -> {
            DictCityVO dictCityVO = new DictCityVO();
            String fullCode = dictCityEntity.getFullCode();
            String cityName = dictCityEntity.getCityName();
            dictCityVO.setCityCode(fullCode);
            dictCityVO.setCityName(cityName);
            return dictCityVO;
        }).collect(Collectors.toList());


        redisUtil.setArray(key,dictCityVOList,RedisUtil.DEFAULT_EXPIRE_TIME);
        return new ResponseBean(200,"查询成功",dictCityVOList);

    }

    @Override
    public ResponseBean getDictCityCounty(String cityFullCode) {
       String key = "dict:city:county"+cityFullCode;
        List<DictCountyVO> dictCountyVOList = redisUtil.getArray(key, DictCountyVO.class);
        if (dictCountyVOList !=null){
            return new ResponseBean(200,"查询成功",dictCountyVOList);
        }


        DictCityEntity dictCityEntity = dictCityDao.selectByCityFullCode(cityFullCode);
        Integer provinceId = dictCityEntity.getProvinceId();

        List<DictCountyEntity> dictCountyEntityList = dictCountryDao.selectByProvinceId(provinceId);
        dictCountyVOList= dictCountyEntityList.stream().map(dictCountyEntity -> {
            DictCountyVO dictCountyVO = new DictCountyVO();
            String fullCode = dictCountyEntity.getFullCode();
            String countyName = dictCountyEntity.getCountyName();
            dictCountyVO.setCountyFullCode(fullCode);
            dictCountyVO.setCountyName(countyName);
            return dictCountyVO;
        }).collect(Collectors.toList());


        return new ResponseBean(200,"查询成功",dictCountyVOList);
    }

    @Override
    public ResponseMessage refreshDict() {
        List<DictCityEntity> dictCityEntityList = sysDictCityService.list();
        List<DictCountyEntity> dictCountyEntityList = sysDictCountyService.list();
        List<DictProvinceEntity> dictProvinceEntityList = sysDictProvinceService.list();

        dictCityEntityMap= CollUtil.fieldValueMap(dictCityEntityList, "fullCode");
        dictCountyEntityMap = CollUtil.fieldValueMap(dictCountyEntityList, "fullCode");
        dictProvinceEntityMap= CollUtil.fieldValueMap(dictProvinceEntityList,"provinceCode");

        return new ResponseMessage(200,"刷新成功");


    }

    @Override
    public String getAddress(String areaId) {
        if (dictProvinceEntityMap.isEmpty() || dictCountyEntityMap.isEmpty() || dictCityEntityMap.isEmpty()){
            refreshDict();
        }
        if (StrUtil.isEmpty(areaId)){
            return "";
        }
        if (areaId.length() ==2){
            return dictProvinceEntityMap.get(areaId).getProvinceName();
        }
        if (areaId.length()==4){
            String provinceCode = StrUtil.sub(areaId, 0, 2);
            String cityCode = StrUtil.sub(areaId,0,4);
            String provinceName = dictProvinceEntityMap.get(provinceCode).getProvinceName();
            String cityName = dictCityEntityMap.get(cityCode).getCityName();
            return provinceName + "-" +cityName;
        }
         if (areaId.length()==6){
             String provinceCode = StrUtil.sub(areaId, 0, 2);
             String cityCode = StrUtil.sub(areaId,0,4);
             String provinceName = dictProvinceEntityMap.get(provinceCode).getProvinceName();
             String cityName = dictCityEntityMap.get(cityCode).getCityName();
             String countyName = dictCountyEntityMap.get(areaId).getCountyName();
             return provinceName + "-" +cityName + "-" + countyName;
         }

        return "";
    }
}
